<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class IndexController extends BaseController {

	public function wxtest(){
		$host = "r-2zea36be464c9504.redis.rds.aliyuncs.com";
		$port = 6379;
		$pwd = "Hrtv2017";
		$redis = new \Redis();
		if($redis->connect($host, $port) == false){
			die($redis->getLastError());
		}
		if($redis->auth($pwd) == false){
			die($redis->getLastError());
		}
		$a1 = 1;
		echo $redis->get("arr_".$a1);
		$key_s = $redis->keys('*');
		dump($key_s);
		foreach($key_s as $key => $v){
			dump($redis->get($v));
		}
	}

	/**
	 * 获取商城图片
	 */
	public function getshopimg(){
		$domainid = I('get.domainid');
		if(!empty($domainid)){
			$type = D(C('TABLE_TYPE'));
			$typeResult = $type->field('shopimg')->where(array('domainid'=>$domainid))->find();
			if(!empty($typeResult)){
				if(!empty($typeResult['shopimg'])){

					$str = "http://h5.hrtv.cn".$typeResult['shopimg'];
				}else{
					$str = "http://h5.hrtv.cn/IndexPublic/images/shopimg.jpg";
				}
			}else{
				$str = "http://h5.hrtv.cn/IndexPublic/images/shopimg.jpg";
			}
		}
		echo "<div class=\"epbanner\"><img src=\"".$str."\" /></div>";
	}

	// 入口首页
	public function index(){
	
		/* 临时session设定值 */
		session('randcode', null);
		session('awardinfo', null);
		session('obj_tel', null);
		$randcode 	= null;
		//当前时间
		$now_time  	= date("Y-m-d H:i:s", time());
		//获取当前地址
		$this->assign('getURL', getURL());
		/* 获取rootid和domainid */
		$rootid_tmp	= I('get.rootid');
		$array_d_r 	= $this->get_rootid_domainid($rootid_tmp);
		$rootid 	= $array_d_r['id'];
		if($rootid == 254){
						echo "<script> window.location.href='http://h5.hrtv.cn/resource/92.html'</script>";
					}
					
					
		if($rootid == 274){
						echo "<script> window.location.href='http://h5.hrtv.cn/resource/125.html'</script>";
					}			
		//echo $rootid;die;
		$domainid 	= $array_d_r['domainid'];
		if(empty($rootid)) $rootid = $rootid_tmp;
		$jumptoid 	= I('get.jumptoid');
		//获取如果存在分享的标识
		$this->assign('mashare', I('get.share'));
		$this->assign('rootid', $rootid);
		session('glob_rootid', $rootid);
		if($rootid == 484) header('LOCATION:http://h5.hrtv.cn/resource/420.html');
		//如果是从入口首页进入
		if($this->get_controller_action() == C('ENTER_ADDRESS')){
			//获取一物一码唯一的标识串
			$randcode = $this->getrandma();
			session('randcode', $randcode);

			$b_result = $array_d_r['isvote'];
			//此码促销时间是有效果
			if($this->is_date_valid($array_d_r, $now_time) == 1 && $b_result == 1){
				//如果存在促销功能则需要微信联合登陆
				$this->islogin(getURL());
				$this->assign('gamerule', $b_result);

				//如果确实是一物一码特性的话则执行以下逻辑
				if(!empty($randcode)){

					$this->assign('isyard', 1);
					//给模板分配唯一标识码串
					$this->assign('randcode', $randcode);
					//预加载获取收货地址信息
					$this->gettakeaddres();
					//根据randcode标识串获取对应的信息
					$randnumber = W('Public/get_randcode_info', array('randcode' => $randcode));
					//判断此码是否被领取
					$islingqu 	= $this->checklingqu($randcode);
					$this->assign('checklingqu', $islingqu);
					//384 383的每个码的领取次数为5次
					if($rootid == 384 || $rootid == 383){
						$award_result = $islingqu >= 0 && $islingqu < 6 ? $this->cxaward1($randnumber['ma_type'], $randcode) : 'noaward';
					}else{
						$award_result = $islingqu != 1 ? W('Public/cx_award', array('ma_type' => $randnumber['ma_type'])) : 'noaward';
					}
					
					
					$this->assign('award', $award_result);
					$this->gameruletologic($b_result, $randcode);
					//获取当前码的企业信息
					$this->assign('eqinfo', $this->epinfotoat($array_d_r['userid']));
					$this->assign('awardjilu', $this->awardjilu($randcode));
				}else{
					//默认为红包
					$award_result = array('awardkind' => C('DEFAULT_WARDKIND'));
					$this->assign('award', $award_result);
				}
			}
		}
		$fuhua_result = $this->get_fuhua_info($rootid);
		if(!empty($fuhua_result['f_video'])){
			$this->checkfuhua_cookie($rootid);
		}
		$this->assign('fuhua', $fuhua_result);
		if(!empty($domainid)){
			$this->showindextpl($rootid, $domainid, $jumptoid);
		}else if(!empty($randcode)){
			$rand = D(C('TABLE_RANDNUMBER'));
			$res  = $rand->where(array('ma_randnumber' => $randcode))->find();
			if(!empty($res)) $this->showindextpl($rootid, $domainid, $res['ma_domainid']);
		}
	}

	/**
	 * 判断浮画是否被浏览过
	 */
	protected function checkfuhua_cookie($rootid){
		$f_cookie = cookie('f_cookie');
		if(!empty($f_cookie)){
			if($f_cookie['rt'] == $rootid){
				$this->assign('fh_cookie', $f_cookie['fh']);
			}else{
				cookie('f_cookie', array('rt' => $rootid, 'fh' => 1));
			}
		}else{
			cookie('f_cookie', array('rt' => $rootid, 'fh' => 1));
		}
	}

	/**
	 * 根据ROOTID获取浮画信息
	 */
	protected function get_fuhua_info($rootid = null){
		$fuhua = D(C('TABLE_FUHUA'));
		$res = $fuhua->where(array('rootid' => $rootid))->find();
		return $res;
	}

	//只有码ID为384或383的时候，才随机查询是什么奖品
	protected function cxaward1($ma_type, $randcode){
		$str 	= null;
		$award 	= D(C('TABLE_AWARD'));
		$res 	= $award->find($ma_type);
		if($res){
			$typeid = $res['typeid'];
			$str 	= $this->typidstr($typeid);
		}
		return $str;
	}

	/**
	 * 扫码领奖品，判断当前码的有效促销时间范围是否有效
	 */
	protected function is_date_valid($array, $now_time = null){
		if($array['yard_startdate'] <= $now_time && $array['yard_enddate'] >= $now_time) return 1;
	}

	protected function typidstr($typeid){
		if(empty($typeid)) return false;
		$str = null;
		$award = D(C('TABLE_AWARD'));
		$r = $award->where(array('typeid'=>$typeid))->select();
		if($r && is_array($r)){
			$str = $r[array_rand($r,1)];
			if($str['awardnum'] == $str['u_num']) $str = $r[array_rand($r,1)];
		}
		session('awardinfo',$str);
		return $str;
	}

	/**
	 * 当前码的获奖记录
	 */
	protected function awardjilu($randcode=null){
		$randhb = D(C('TABLE_RANDNUMBER_HB'));
		$res 	= $randhb->where(array('ma_randnumber'=>$randcode))->select();
		if($res){
			return $res;
		}else{
			$telinfo = D(C('TABLE_TELINFO'));
			$res = $telinfo->where(array('ma_randinfo'=>$randcode))->select();
			if($res){
				return $res;
			}
		}
	}

	/**
	 * 判断此码是否被领取
	 */
	protected function checklingqu($randcode=null){
		$rand 	= D(C('TABLE_RANDNUMBER'));
		$res 	= $rand->field('ma_hb_cishu')->where(array('ma_randnumber'=>$randcode))->find();
		if($res) $r = $res['ma_hb_cishu'];
		return $r;
	}

	/**
	 * 获取此码是属于什么类型抽奖规则
	 */
	protected function getmagamerule($randcode){
		$rand 	= D(C('TABLE_RANDNUMBER'));
		$res 	= $rand->where(array('ma_randnumber'=>$randcode))->find();
		return $res;
	}

	/**
	 * 查询是什么奖品
	 */
	protected function cxaward($randcode){
		$res 	 = $this->getmagamerule($randcode);
		$award 	 = D(C('TABLE_AWARD'));
		if($res['ma_type']!=1){
			$res = $award->find($res['ma_type']);
			return $res;
		}else{
			return array('id'=>1);
		}
	}

	/**
	 * 获取收货地址信息
	 */
	protected function gettakeaddres(){
		$userinfo 	= $_COOKIE['user_userinfo'];
		$telres 	= D(C('TABLE_TELINFO'));
		$res 		= $telres->field('id,marootid,useropenid,tel,username,address')->where(array('useropenid'=>$userinfo))->limit(1)->find();
		$this->assign('reginfo',$res);
	}

	/**
	 * 根据不同账号发出的码获取当前企业信息
	 */
	protected function epinfotoat($userid = null){
		$userinfo 	= D(C('TABLE_USERINFO'));
		$result 	= $userinfo->where(array('user_id'=>$userid))->find();
		return $result;
	}

	/**
	 * 根据不同账号发出的码获取当前红包接口信息
	 */
	protected function userid_portin($user_id = null){
		$hbinfo = D(C('TABLE_HBINFO'));
		$result = $hbinfo->where(array('user_id' => $user_id))->find();
		return $result;
	}

	/**
	 * 根据不同账号发出的码获取用户ID
	 */
	protected function randnumbertouserid($randcode){
		$rand 	= D(C('TABLE_RANDNUMBER'));
		$res 	= $rand->where(array('ma_randnumber'=>$randcode))->find();
		if($res){
			$marootid 	= $res['ma_rootid'];
			$type 		= D(C('TABLE_TYPE'));
			$r 			= $type->find($marootid);
			if($r){
				$userid = $r['userid'];
				return $userid;
			}
		}
	}

	/**
	 * 不同游戏执行不同逻辑
	 */
	protected function gameruletologic($n, $randcode){
		if($n==1){
			$randhb = D(C('TABLE_RANDNUMBER_HB'));
			$res 	= $randhb->where(array('ma_randnumber'=>$randcode))->select();
			if($res){
				/*判断，如果获取了前但是没有领走，然后根据openid去查询此用户有没有被领取*/
				foreach ($res as $v){
					/*如果红包没有被领取再执行以下操作*/
					if($v['ma_money_state']==1){
						$this->ishbzou($randcode, $v['ma_mch_billno'], $v['id']);
					}
				}
			}
		}
	}

	/**
	 * 判断红包是否被领取
	 */
	protected function ishbzou($randcode,$ma_mch_billno,$id){
		$confirm 			= md5($ma_mch_billno.'cf@huaren888');
		$getmoenystateurl 	= "http://pay.hrtv.cn/hongbao/gethbinfo?mch_billno=".$ma_mch_billno."&confirm=".$confirm;
		$jsonresult 		= file_get_contents($getmoenystateurl);
		$jsonresult 		= trim($jsonresult, "\xEF\xBB\xBF");
		$jsonresult 		= json_decode($jsonresult,true);
		/*如果已领取*/
		$m_status = trim($jsonresult['status']);
		if($m_status=='RECEIVED'){
			$this->setmoneystates($m_status,2,$jsonresult['mch_billno'], $randcode, $id);
		}else if($m_status=='REFUND'){ //如果已退款
			$this->setmoneystates($m_status,null,$jsonresult['mch_billno'], $randcode, $id);
		}
	}

	/**
	 * 如果已领取更新数据库状态
	 */
	protected function setmoneystates($hbstatus,$moneystatus,$mch_billno,$randcode,$id){
		$rand = D(C('TABLE_RANDNUMBER_HB'));
		if($hbstatus == 'RECEIVED'){
			//如果已领取则更新领取的状态
			$data['ma_money_state'] = $moneystatus;
			$res = $rand->where(array('ma_mch_billno'=>$mch_billno,'ma_randnumber'=>$randcode,'id'=>$id))->save($data);
		}else if($hbstatus=='REFUND'){
			//如果退回则删除这条记录
			$res = $rand->delete($id);
			//接着执行此码的扫码次数
			$num = $rand->where(array('ma_randnumber'=>$randcode))->count();
			if($num > 1){
				/*判断是否被领过大于一次 +2*/
				$this->setmahbcishu($randma,2);
			}else if($num = 1){
				/*判断是否被领过一次 +1*/
				$this->setmahbcishu($randma,1);
			}else{
				/*没有记录就清零*/
				$this->setmahbcishu($randma,0);
			}
		}
		return $res;
	}

	protected function showindextpl($rootid, $domainid, $jumptoid=null){
		switch ($domainid){
			case 520:
				echo "<script>location.href='http://h5.hrtv.cn/rootid/24';</script>";
				break;
			case 684:
				echo "<script>location.href='http://h5.hrtv.cn/rootid/25';</script>";
				break;
			case 720:
				echo "<script>location.href='http://h5.hrtv.cn/rootid/68';</script>";
				break;
			case 775:
				header('Location:http://e.hrtv.cn/ehrtv/mrys/newindex?id=436&domain='.$domainid.'&bottomType=2&domain='.$domainid);
				$this->assign('fd775',1);
				break;
		}
		//获取商品信息

		$dm = $domainid;
		if($domainid == 1059) $domainid = 973;
		$arrdoid = array('1088','1090','1091','1092','1093','1094');
		$arrdoid1 = array('1086','1083','1085','1084','1081','1082');
		if(in_array($domainid, $arrdoid)) $domainid = 1088;
		if(in_array($domainid, $arrdoid1)) $domainid = 973;
		$this->assign('shoplist',get_shopinfo($domainid));
		$this->assign('domainid', $domainid);
		if(empty($jumptoid)) $jumptoid = 267545;  //给一个默认值，方便后台预览观看效果
		$type 		= D(C('TABLE_TYPE'));
		$typeResult = $type->where(array('domainid'=>$dm))->find();
		if(!$typeResult['state']) $this->alerterror(C("MSG_AUDIT"));
		/*广告推送内容*/
		$this->getadinfo($typeResult['adid']);
		$str 		= "http://e.hrtv.cn/build/index.php?c=view&a=indextest1&domainid=".$dm."&id=".$jumptoid;
		$resultStr 	= file_get_contents($str);
		$this->assign('publicButtom',$resultStr);
		//分享开始
		$this->sharecontent($dm);

		$this ->getMa($rootid);
		$this ->assign('type',$typeResult);
		$tpls = $typeResult['template'];
		if(!empty($tpls)){
			if($tpls == "index6"){
				$submenu   = D(C('TABLE_SUBMENU'));
				$res 	   = $submenu->where(array('mtypeid' => $rootid,'position' => 1))->find();
				if(!empty($res)){
					$article = D(C('TABLE_ARTICLE'));
					$ares    = $article->where(array('menuid' => $res['id']))->select();
					$this->assign('articleReuslt',$ares);
					$this->assign('bottom',$this->bottomout($rootid, $dm));
				}
			}
			$tpl = $tpls;
		}else{
			$tpl = "index";
		}
		$this->assign('getURL',getURL());
		$this->assign('md5confirm',md5($_COOKIE['user_userinfo'].'cf@huaren888'));

		//输出模板
		if($tpl=='index8'){
			$article 	= D(C('TABLE_ARTICLE'));
			$page 		= I('get.page');
			$rootid 	= I('get.rootid');
			$domainid 	= I('get.domainid');
			if(empty($page)){
				$res 	= $article->where(array('typeid'=>$rootid))->order('pubdate desc')->limit(1)->find();
			}else if(!empty($page)){
				$res 	= $article->where(array('typeid'=>$rootid,'id'=>$page))->order('pubdate desc')->find();
			}
			if($res){
				$this 	->assign('resinfo',$res);
				$page 	= $res['id'];
				//下一条
				$after 	= $article->where("id>$page and typeid=$rootid")->order('id desc')->limit('1')->find();
				$this 	->assign('after',U('index',array('domainid'=>$domainid,'rootid'=>$rootid,'page'=>$after['id'])));
				//上一条
				$front 	= $article->where("id<$page and typeid=$rootid")->order('id desc')->limit('1')->find();
				$this 	->assign('front',U('index',array('domainid'=>$domainid,'rootid'=>$rootid,'page'=>$front['id'])));
			}
		}
		if($tpl == 'index9' || $tpl == 'index10'){
			/* 获取自定义版块 */
			//根据rootid获取此条信息
			$type_info  = $this->id_get_type_info($rootid);
			//企业或政府版块
			$first_article = $this->get_rootid_article_list($rootid);
			$this->assign('article_info', $first_article);
			$plate_result = $this->get_custom_plate($type_info['plate_id']);
			if($plate_result){
				//政府版块
				$this->assign('zhengfu_resource_info', $this->get_zhengfu_info($plate_result));
				//自定义资源版块
				$this->assign('custom_resource_info', $this->get_custom_info($plate_result));
				$this->assign('plate_resource_info', $plate_result);
				foreach($plate_result as $v){
					$this->plateid_contentlist($v['id']);
				}
			}
		}

		if($tpl == 'index7'){
			$User = M("article"); 
			// 如果以后其他要用这个模板，可以多加一个字段，判断是哪个语言去判断查询数据
			$where_t1 = array('menuid' =>4672 ,'typeid' =>633,'rtype' =>'1','extendattr'=>'t');
			$list_t1 = $User->where($where_t1)->select();
			//var_dump($list_t1['0']);die;
			$where = array('menuid' =>4672 ,'typeid' =>633);
			$list = $User->where($where)->select();
			//var_dump($list);die;
			//1新闻也就是鸟瞰中国
			$where1 = array('menuid' =>4672 ,'typeid' =>633,'rtype' =>'1','extendattr'=>'');
			$list1 = $User->where($where1)->order('id','desc')->limit(4)->select();
			//2文化中国茶
			$where2 = array('menuid' =>4672 ,'typeid' =>633,'rtype' =>'2');
			$list2 = $User->where($where2)->limit(6)->select();
			//3热点中国功夫
			$where3 = array('menuid' =>4672 ,'typeid' =>633,'rtype' =>'3');
			$list3 = $User->where($where3)->limit(4)->select();
			//4娱乐中国旅游
			$where4 = array('menuid' =>4672 ,'typeid' =>633,'rtype' =>'4');
			$list4 = $User->where($where4)->limit(4)->select();
			//5要闻中国食物
			$where5 = array('menuid' =>4672 ,'typeid' =>633,'rtype' =>'5');
			$list5 = $User->where($where5)->limit(4)->select();

			//6推介中国
			$where6 = array('menuid' =>4672 ,'typeid' =>633,'rtype' =>'6');
			$list6 = $User->where($where6)->limit(4)->select();

			$info = array('1' =>$list1 ,'2' =>$list2 ,'3' =>$list3,'4' =>$list4 ,'5' =>$list5,'6'=>$list6);
		
			//var_dump($list6);die;
			$this->assign('list_t1',$list_t1);
			$this->assign('list1',$list1 );
			$this->assign('list2',$list2 );
			$this->assign('list3',$list3 );
			$this->assign('list4',$list4 );
			$this->assign('list5',$list5 );
			$this->assign('list6',$list6 );

		}

		$this->display($tpl);
	}

	/*=================================小程序接口开始==========================================*/
	public function wx_api_getcustomplate($plate_id = array(53,56,85,220,90,89)){
		//根据rootid获取所属自定义版块ID
		$plate = D(C('TABLE_PLATEMANAGES'));
		$where['id'] = array('in', $plate_id);
		$result_array = $plate->where($where)->order('plate_sort DESC')->select();
		if($result_array){
			foreach($result_array as $k => &$v){
				if($v['plate_type'] != 2){
					unset($result_array[$k]);
				}else{
					$v['custom_subinfo'] = $this->wx_getplatesubcontent($v['id']);
				}
			}
		}
		echo json_encode($result_array);
	}
	protected function wx_getplatesubcontent($plate_id = null){
		$platecontent = D(C('TABLE_PLATECONTENT'));
		$res = $platecontent->where(array('plate_id' => $plate_id, 'state' => 1))->limit(2)->order('editdate DESC,id DESC')->select();
		if($res){
			foreach($res as &$v){
				$v['description'] = msubstr($v['description'], 30);
			}
		}
		return $res;
	}

	public function wx_getrootidarticlelist($rootid = 447){
		$article 	= D(C('TABLE_ARTICLE'));
		$res = $article->where(array('typeid' => $rootid))->order('editdate desc,id desc')->select();
		echo json_encode($res);
	}
	/*=================================小程序接口结束==========================================*/

	/**
	 * 判断是否有自定义资源的版块，有则获取信息
	 */
	protected function get_custom_info($result_array = null){
		foreach($result_array as $k => &$v){
			if($v['plate_type'] != 2){
				unset($result_array[$k]);
			}else{
				$v['custom_subinfo'] = $this->get_plate_subcontent($v['id']);
			}
		}
		return $result_array;
	}

	/**
	 * 根据PLATEID获取文章信息
	 */
	protected function get_plate_subcontent($plate_id = null){
		$platecontent = D(C('TABLE_PLATECONTENT'));
		$limit = 2;
		if($plate_id == 220) $limit = 3;
		$res = $platecontent->where(array('plate_id' => $plate_id, 'state' => 1))->limit($limit)->order('editdate DESC,id DESC')->select();
		return $res;
	}

	/**
	 * 判断是否有政府的版块，有则获取信息
	 */
	protected function get_zhengfu_info($result_array = null){
		foreach($result_array as $k => $v){
			if($v['plate_type'] != 3){
				unset($result_array[$k]);
			}
		}
		return $result_array;
	}

	/**
	 * 根据rootid获取文章列表
	 */
	protected function get_rootid_article_list($rootid = null){
		$article 	= D(C('TABLE_ARTICLE'));
		$res = $article->where(array('typeid' => $rootid))->order('editdate desc,id desc')->select();
		return $res;
	}

	/**
	 * 底部输出
	 */
	protected function bottomout($rootid,$domainid){
		$botres = W('Bottom/getbottommenu',array('rootid'=>$rootid));
		if($botres){
			foreach($botres as &$v){
				$v['jumpaddres'] = str_replace('#domainid#',$domainid,$v['jumpaddres']);
			}
		}
		return $botres;
	}

	protected function shiwurestrict($randcode){
		$randnumber = D(C('TABLE_RANDNUMBER'));
		$res 		= $randnumber->field('iswhoget')->where(array('ma_randnumber' => $randcode))->find();
		$iswhoget 	= $res['iswhoget'];
		if($iswhoget){
			return $this->shiwurestrictc($iswhoget);
		}else{
			return false;
		}
	}
	protected function shiwurestrictc($iswhoget){
		$randnumber = D(C('TABLE_RANDNUMBER'));
		$count 		= $randnumber->where("iswhoget='".$iswhoget."' and to_days(zjtime) = to_days(now()) and ma_rootid=275")->count();
		if($count > 1){
			//当天已经领过2次
			return true;
		}
	}

	/**
	 * 分享逻辑
	 */
	protected function sharecontent($domainid){
		$r = $this->getsubmenu($domainid);
		foreach ($r as $v){
			if($v['position'] == 1){
				$v['geturl']   = rtrim(getURL(),'=');
				$v['thumbimg'] = C('WEBDOMAIN').$v['thumbimg'];
				//dump($v);
				$this->assign('shareinfo',$v);
			}
		}
	}

	/**
	 * 获取广告推送信息
	 */
	protected function getadinfo($id){
		if(!empty($id)){
			/* 1 */
			$tpl = "<div class=\"swiper-container\" style=\"margin-top:3px;\"><div class=\"swiper-wrapper\">";
			$tp2 = "</div><div class=\"swiper-pagination\"></div></div>";
			$style 	= null; //定义样式变量
			$str 	= null;
			$arrayid = explode(',', $id);
			$ad = D(C('TABLE_AD'));
			$where['id'] = array('in', $id);
			$getres      = $ad->where($where)->select();
			foreach ($getres as $res){
				if($res['ad_push']!=0){
					$str .= "<div class=\"swiper-slide\"><a href=\"".$res['ad_link']."\"><img src='".$res['ad_img']."' width='100%'></a></div>";
				}
			}
			if(!empty($str)){
				$str1 = $tpl.$str.$tp2;
			}else{
				$str1 = $str;
			}
		}else{
			$str1 = null;
		}
		$this->assign('adinfo',$str1);
	}

	/* 政府 */
	public function list2(){
		$id  = I('get.id');
		$this->detectionMaClose($id); // 检测此码是否被关闭
		$this->getMa($id);
		$this->display('zhengfu');
	}

	/* 企业 */
	public function list1(){
		$id = I('get.id');
		$this->pubort($id);
		$rootid 	= W('Bottom/getrootidanddomainid',array('rootid' => 'rootid', 'domainid' => '-', 'articleid' => $id));
		$domainid 	= W('Bottom/getrootidanddomainid',array('rootid' => '-', 'domainid' => 'domainid', 'articleid' => $id));
		$this->assign('bottom',$this->bottomout($rootid,$domainid));
		$this->display($this->geterjitpl($id));
	}

	public function list3(){
		$id = I('get.id');
		//echo $articleReuslt;
		$this->pubort($id);
		$this->display('other1');
	}

	public function list4(){
		$id = I('get.id');
		//echo $articleReuslt;
		$this->pubort($id);
		$this->display('other2');
	}

	public function list5(){
		$id = I('get.id');
		//echo $articleReuslt;
		$this->pubort($id);
		$this->display('other3');
	}

	public function list6(){
		$id = I('get.id');
		//echo $articleReuslt;
		$this->pubort($id);
		$this->display('other3');
	}

	protected function geterjitpl($id){
		$submenu = D(C('TABLE_SUBMENU'));
		$res     = $submenu->find($id);
		if($res){
			switch ($res['classtype']){
				case 1:
					return 'qiye';
					break;
				case 3:
					return 'other1';
					break;
				case 4:
					return 'other2';
					break;
				case 5:
					return 'other3';
					break;
				case 6:
					return 'other4';
					break;
				default:
					return 'qiye';
			}
		}else{
			return 'qiye';
		}
	}

	public function pubort($id){
		// 检测此码是否被关闭
		$submenu 		= D(C('TABLE_SUBMENU'));
		$submenuResult 	= $submenu->where(array('id' => $id))->find();
		if(!empty($submenuResult)){
			$type = D(C('TABLE_TYPE'));
			$typeResult = $type->where(array('id' => $submenuResult['mtypeid']))->find();
			if(!$typeResult['state']) $this->alerterror(C("MSG_AUDIT"));
			$this->getMa($typeResult['id']);
			$this->assign('domainid',$typeResult['domainid']);
		}
	}

	/* 文化内容 */
	public function whlist(){
		$this -> getplateallinfo();
		$this -> display();
	}

	/* 活动 */
	public function huodong(){
		$this -> getplateallinfo();
		$this -> display();
	}
	protected function getplateallinfo(){
		$plateres 	= D(C('TABLE_PLATERESOURCE'));
		$res 		= $plateres->order(array('id'=>'desc'))->select();
		$this->assign('data',$res);
	}

	// 判断是否具有相同的信息
	protected function checkhblqinfo($randcode,$openid){
		$telres = D(C('TABLE_RANDNUMBERHB'));
		$count  = $telres->where(array('ma_randnumber'=>$randcode,'ma_user_openid'=>$openid))->count();
		if($count>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 根据randcode查询奖品ID及rootid
	 * $randcode
	 * $filed 字段名
	 */
	protected function randcodetoawardid($randcode,$filed){
		$randnumber = D(C('TABLE_RANDNUMBER'));
		$res 		= $randnumber->field('ma_rootid,ma_type')->where(array('ma_randnumber'=>$randcode))->find();
		if($res){
			return $res[$filed];
		}else{
			return null;
		}
	}

	/**
	 * 根据奖品ID获取红包金额范围
	 * 如果为3（红包）则返回红包的最小和最大金额
	 */
	protected function awardidtohbmoney($awardid){
		$award = D(C('TABLE_AWARD'));
		$res   = $award->field('awardkind,u_hbmin,u_hbmax')->where(array('id'=>$awardid))->find();
		if($res){
			$res1 = $res;
			foreach($res1 as $v){
				if($v==3){
					return $res;
				}else{
					return null;
				}
			}
		}else{
			return null;
		}
	}

	// 测试的，没啥用，后期有用吧
	public function ceshi(){
		$z 			= strtoupper(I('get.z'));
		$randnumber = D(C('TABLE_TYPE'));
		$res 		= $randnumber->field('epname')->select();
		if($res){
			foreach($res as $key=>$v){
				$name = trim($v['epname']);
				//echo $z.' === '.$v['epname'].' —— '.$this->getfirstchar($name).'<br>';
				if($this->getfirstchar($name) == $z){
					echo $name.'<br>';
				}
			}
		}
	}
	protected function getfirstchar($s0){
		$fchar = ord($s0{0});
		if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
		$s1 = iconv("UTF-8","gb2312", $s0);
		$s2 = iconv("gb2312","UTF-8", $s1);
		if($s2 == $s0){$s = $s1;}else{$s = $s0;}
		$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
		if($asc >= -20319 and $asc <= -20284) return "A";
		if($asc >= -20283 and $asc <= -19776) return "B";
		if($asc >= -19775 and $asc <= -19219) return "C";
		if($asc >= -19218 and $asc <= -18711) return "D";
		if($asc >= -18710 and $asc <= -18527) return "E";
		if($asc >= -18526 and $asc <= -18240) return "F";
		if($asc >= -18239 and $asc <= -17923) return "G";
		if($asc >= -17922 and $asc <= -17418) return "H";
		if($asc >= -17417 and $asc <= -16475) return "J";
		if($asc >= -16474 and $asc <= -16213) return "K";
		if($asc >= -16212 and $asc <= -15641) return "L";
		if($asc >= -15640 and $asc <= -15166) return "M";
		if($asc >= -15165 and $asc <= -14923) return "N";
		if($asc >= -14922 and $asc <= -14915) return "O";
		if($asc >= -14914 and $asc <= -14631) return "P";
		if($asc >= -14630 and $asc <= -14150) return "Q";
		if($asc >= -14149 and $asc <= -14091) return "R";
		if($asc >= -14090 and $asc <= -13319) return "S";
		if($asc >= -13318 and $asc <= -12839) return "T";
		if($asc >= -12838 and $asc <= -12557) return "W";
		if($asc >= -12556 and $asc <= -11848) return "X";
		if($asc >= -11847 and $asc <= -11056) return "Y";
		if($asc >= -11055 and $asc <= -10247) return "Z";
		return null;
	}


	/**
	 * 文章搜索
	 */
	public function search(){
		$key = $_GET['key'];
		$key = urldecode($key);
		$key = mb_convert_encoding($key, "UTF-8", "gb2312");
		$id  = I('get.id');
		$where['menuid'] = $id;
		$where['title']  = array('like',"%$key%");
		$article = D(C('TABLE_ARTICLE'));
		$articleReuslt = $article->where($where)->order('weight desc')->select();
		$this->assign('articleReuslt',$articleReuslt);
		$this->pubort($id);
		$this->display('other1');
	}

	/* ============================领取奖品开始============================ */
	public function getaward(){
		// 首先判断是否经过微信授权
		$this->islogin(getURL());
		// 获取此码标识
		$randcode = session('randcode');
		if(empty($randcode)) die('参数错误');
		// 获取此码的数据信息
		$randnumber = W('Public/get_randcode_info',array('randcode' => $randcode));
		// 获取批次
		$batchnumber = $randnumber['batchnumber'];
		// 获取此码对应的rootid
		$rootid = $randnumber['ma_rootid'];
		// 获取此码的奖品领取次数
		$cishu = $randnumber['ma_hb_cishu'];
		if($rootid == 384 || $rootid == 383){
			$visitcount = $this->check_restrict1($rootid,$randcode);
			if($visitcount){
				$res = array('type'=>0,'message'=>'您已经领取过一次奖品');
			}else{
				if($cishu==5){
					$res = array('type'=>33,'message'=>'此奖品已被人领取');
				}else{
					$res = array('type'=>2,'message'=>$randcode);
				}
			}
		}else{
			$visitcount = $this->check_restrict($randcode);
			if($visitcount){
				$res = array('type'=>0,'message'=>'每天只能领取两次');
			}else{

				if($cishu == 1){
					$res = array('type'=>3,'message'=>'此奖品已被人领取');
				}else{
					// 获取此码所对应的奖品id
					$awardid  = $randnumber['ma_type'];
					// 获取奖品信息
					$awarinfo = $this->get_award_info($awardid);
					// 获取奖品类型3红包，2实物，1华人钻
					$awardind = $awarinfo['awardkind'];
					switch ($awardind){
						case 3:
							// 根据rootid获取此码所对应的用户userid
							$mainfo = $this->get_ma_info($rootid);
							$userid = $mainfo['userid'];
							// 根据userid获取微信商户平台的接口信息
							$appid = $this->get_useridtoweixinapi_info('u_appid',$userid);
							// 获取金额最小、最大
							$u_hbmin = $awarinfo['u_hbmin'];
							$u_hbmax = $awarinfo['u_hbmax'];
							$res = $this->new_sendhb($rootid,$randcode,$appid,$u_hbmin,$u_hbmax,$visitcount,$batchnumber);
							break;
						case 2:
							$res = array('type'=>2,'message'=>$randcode);
							break;
						case 1:
							$mainfo = $this->get_ma_info($rootid);
							$msg = '来自'.$mainfo['epname'].'的抽奖获得';
							$r = $this->adddrill($randcode,$msg);
							if($r){
								$res = array('type'=>1,'diamond'=>$r);
							}
							break;
						case 5:
							$res = array('type'=>5,'message'=>$randcode);
							break;
						case 4:
							$res = array('type'=>4,'message'=>$randcode);
							break;
					}
				}
			}
		}
		echo json_encode($res);
	}

	/**
	 * 针对洛娃码，实行一个手机每天只能领取两次奖
	 */
	protected function check_restrict($randcode=null){
		if(!empty($randcode)){
			$result 	= false;
			$randnumber = D(C('TABLE_RANDNUMBER'));
			$userinfo 	= $_COOKIE['user_userinfo'];
			$count 		= $randnumber->where("iswhoget='".$userinfo."' and to_days(zjtime) = to_days(now()) and ma_rootid=275")->count();
			if($count > 1){
				$result = true;
			}else{
				$telinfo = D(C('TABLE_TELINFO'));
				$r = $telinfo->where(array('useropenid'=>$userinfo))->limit(1)->order('id desc')->find();
				if($r){
					$whoid = $r['id'];
					$rand  = D(C('TABLE_RANDNUMBER'));
					$count = $rand->where("iswhoget='".$whoid."' and to_days(zjtime) = to_days(now()) and ma_rootid=275")->count();
					if($count>1){
						$result = true;
					}
				}
			}
			return $result;
		}
	}

	protected function check_restrict1($rootid,$randcode=null){
		if(!empty($randcode)){
			$result   = false;
			$userinfo = $_COOKIE['user_userinfo'];
			$telinfo  = D(C('TABLE_TELINFO'));
			$r = $telinfo->where(array("useropenid = '".$userinfo."' and marootid = $rootid"))->limit(1)->order('id desc')->count();
			if($r>0) $result = true;
			return $result;
		}
	}

	/**
	 * 根据抽奖获取的华人钻，添加至个人账户里
	 */
	protected function adddrill($randcode,$msg){
		$diamond  = rand(5, 10);
		$userinfo = $_COOKIE['user_userinfo'];
		$r = request_get(C('APIADDRES1')."/addscore?token=".$userinfo."&score=".$diamond."&msg=".urlencode($msg));
		if($r['ret']==1){
			$status = $this->changeawardstatus($userinfo,$randcode);
			if($status) return $diamond;
		}else{
			return false;
		}
	}

	/**
	 * 发送红包
	 */
	protected function new_sendhb($rootid,$randcode,$appid,$u_hbmin,$u_hbmax,$visitcount,$batchnumber){
		$this->islogin(getURL());
		if($visitcount>1){
			$res = array('type'=>3,'message'=>'超过领取次数');
		}else{
			$act_name 	= "华人频道红包计划";
			$send_name 	= "华人频道";
			$remark 	= "华人";
			$wishing 	= "华人";
			if($rootid==275){
				$act_name 	= "洛娃洗衣液红包计划";
				$send_name 	= "洛娃集团";
				$remark 	= "洛娃集团";
				$wishing 	= "洛娃集团";
			}
			$re_openid 	= $_COOKIE['user_userinfo'];
			$confirm 	= md5($re_openid.'cf@huaren888');
			// $rootid = $this->getdomaidandrootid(null)['id'];
			// 判断如果奖品中设置了发红包的金额范围则获取
			if(empty($u_hbmin)) $u_hbmin = 1;
			if(empty($u_hbmax)) $u_hbmax = 1.03;
			$hbapiurl = "http://pay.hrtv.cn/hongbao/sendredpack?u_hbmin=$u_hbmin&u_hbmax=$u_hbmax&re_openid=$re_openid&act_name=$act_name&send_name=$send_name&remark=$remark&wishing=$wishing&confirm=$confirm&appid=$appid";
			$str = file_get_contents($hbapiurl);
			$str = trim($str, "\xEF\xBB\xBF");
			$arrayresult = json_decode($str,true);

			if($arrayresult['statuss']==1){
				$total_amount = $arrayresult['total_amount']/100;
				$res = array('type'=>3,'message'=>'恭喜您获得'.$total_amount.'元红包');
				// 记录红包领取记录
				$upstatus = $this->changeawardstatus($re_openid,$randcode);
				if($upstatus){
					$this->savehbrecord($re_openid,$rootid,$randcode,$total_amount,$arrayresult['mch_billno'],$batchnumber);
				}
			}else if($arrayresult['info']=="系统错误:帐号余额不足，请到商户平台充值后再重试"){
				$res = array('type'=>3,'message'=>'红包已抢完！');
			}else if($arrayresult['info']=="系统错误:发放失败，此请求可能存在风险，已被微信拦截"){
				$res = array('type'=>3,'message'=>'红包已抢完！');
			}else{
				$res = array('type'=>3,'message'=>'红包已抢完！');
			}
		}
		return $res;
	}

	/**
	 * 改变此码的奖品领取状态
	 */
	protected function changeawardstatus($re_openid,$randcode=null){
		$randnumberinfo = W('Public/get_randcode_info',array('randcode' => $randcode));
		$marootid = $randnumberinfo['ma_rootid'];

		$rand = D(C('TABLE_RANDNUMBER'));
		if($marootid == 384 || $marootid == 383){
			if($marootid['ma_hb_cishu'] != 5){
				$data['ma_hb_cishu'] = array('exp','ma_hb_cishu+1');
			}
			// 查询奖品消耗次数
			$awardinfo = session('awardinfo');
			if($awardinfo['u_num'] != $awardinfo['awardnum']){
				$this->record_award_consume_num($awardinfo['id']);
				// 给中奖者发送短信
				$tel = session('obj_tel');
				$message = $awardinfo['awardname'];
				if($marootid == 384){
					$addres = '济阳县龙海路224号（县交通局对面），热线电话：0531-84234778';
				}else if($marootid == 383){
					$addres = '龙海路18号红芳尊专卖店，热线电话：0531-84556789';
				}
				$this->send_award_info($message,$addres,$tel);
			}
		}else{
			$data['ma_hb_cishu'] 	= 1;
			$data['iswhoget'] 		= $re_openid;
			$data['zjtime'] 		= date("Y-m-d H:i:s", time());
		}
		$res = $rand->where(array('ma_randnumber'=>$randcode))->save($data);
		return $res;
	}

	/**
	 * 给中奖者发送短信
	 */
	protected function send_award_info($message,$addres,$tel = null){
		if(empty($tel)) return false;

		$fun= new \Org\Util\fun();
		$fun->Sendnote($tel,$message,$addres);
	}

	/**
	 * 记录奖品消耗次数
	 */
	protected function record_award_consume_num($id = null){
		if(empty($id)){
			return false;
		}else{
			$award = D(C('TABLE_AWARD'));
			$award->u_num = array('exp','u_num+1');
			$res = $award->where(array('id'=>$id))->save();
			if($res){
				return true;
			}
		}
	}

	/**
	 * 插入领取红包的记录
	 */
	protected function savehbrecord($re_openid,$rootid,$randcode,$total_amount,$ma_mch_billno,$batchnumber){
		$checkinfo = $this->checkinfo($randcode,$re_openid);
		if(!$checkinfo){
			$randhb = D(C('TABLE_RANDNUMBER_HB'));
			$data['marootid'] = $rootid;
			$data['ma_randnumber'] 		= $randcode;
			$data['ma_money'] 			= $total_amount;
			$data['ma_money_state'] 	= 1;  // 1获取但没领取  2已领
			$data['ma_user_openid'] 	= $re_openid;
			$data['ma_mch_billno'] 		= $ma_mch_billno;
			$data['ma_randnumber_date'] = date("Y-m-d H:i:s", time());
			$data['batchnumber'] 		= $batchnumber;
			$res = $randhb->add($data);
			return $res;
		}
	}
	protected function checkinfo($randcode, $openid){
		$randhb = D(C('TABLE_RANDNUMBER_HB'));
		$c      = $randhb->where(array('ma_randnumber' => $randcode,'ma_user_openid' => $openid))->count();
		return $c > 0 ? true : false;
	}
	/**
	 * 根据奖品id获取对应的奖品信息
	 */
	protected function get_award_info($awardid = null){
		$r 		= null;
		$rand 	= D(C('TABLE_AWARD'));
		$res 	= $rand->field('id,typeid,awardname,awardkind,u_hbmin,u_hbmax,userid')->find($awardid);
		if($res){
			$r  = $res;
		}
		return $r;
	}
	/**
	 * 根据randcode获取对应的码台数据信息
	 */
	protected function get_ma_info($marootid=null){
		$r 		= null;
		$rand 	= D(C('TABLE_TYPE'));
		$res 	= $rand->field('typename,userid')->find($marootid);
		if($res){
			$r  = $res;
		}
		return $r;
	}
	/**
	 * 根据userid获取微信商户平台的接口信息
	 */
	protected function get_useridtoweixinapi_info($field,$userid=null){
		$r 		= null;
		$rand 	= D(C('TABLE_HBINFO'));
		$res 	= $rand->where(array('user_id'=>$userid))->find();
		if($res){
			$r = $res[$field];
		}
		return $r;
	}


	/* ========================保存收货地址======================== */
	/**
	 * 保存注册信息
	 */
	public function addresreg(){
		$randcode 		= I('post.randcode');
		$randnumberinfo = W('Public/get_randcode_info',array('randcode' => $randcode));
		// 获取批次
		$batchnumber 	= $randnumberinfo['batchnumber'];
		$marootid 		= $randnumberinfo['ma_rootid'];
		$openid 		= $_COOKIE['user_userinfo'];
		$usertel 		= I('post.usertel');
		$username 		= I('post.username');
		$address 		= I('post.address');

		$checkinfo = $this->checkaddresinfo($randcode,$openid);
		if(!$checkinfo){
			$telres = D(C('TABLE_TELINFO'));
			$data['marootid'] 		= $marootid;
			$data['ma_randinfo'] 	= $randcode;
			$data['useropenid'] 	= $openid;
			$data['tel'] 			= $usertel;
			$data['username'] 		= $username;
			$data['address'] 		= $address;
			$data['regdate'] 		= date("Y-m-d H:i:s", time());
			$data['batchnumber']	= $batchnumber;
			$res_num = $telres->add($data);
			if($res_num){
				session('obj_tel',$usertel);
				// 执行改变此码的奖品领取状态
				$status = $this->changeawardstatus($res_num,$randcode);
				if($status) $result = array('message'=>'提交成功');
			}else{
				$result = array('message'=>'提交失败，请重新填写！');
			}
		}else{
			session('obj_tel',$checkinfo['tel']);
			$status = $this->changeawardstatus($checkinfo,$randcode);
			if($status) $result = array('message'=>'提交成功');
		}
		echo json_encode($result);

	}
	/**
	 * 判断是否具有相同的信息
	 */
	protected function checkaddresinfo($randcode,$openid){
		$result = false;
		$telres = D(C('TABLE_TELINFO'));
		$res 	= $telres->where(array('ma_randinfo'=>$randcode,'useropenid'=>$openid))->find();
		if($res){
			$result = $res['id'];
		}
		return $result;
	}

}